﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;

public partial class admin_NetDiskSetting_NetDiskCatalogOpUserEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString.Get("nd_id") != null)
            {
                int ndid = int.Parse(Request.QueryString.Get("nd_id"));
                NetDisktbServices netDisktbService = new NetDisktbServices();
                NetDisktb netDisk = netDisktbService.findByNdId(ndid);
                string pcps = netDisk.Pcps;
                string opUsers = netDisk.NetDiskCatalogOpUsers;
                string[] opUser = null;
                if (opUsers != null && !"".Equals(opUsers))
                {
                    opUser = opUsers.Split(',');
                }
                string[] pcs = pcps.Split(',');
                this.BtnSubmit.Attributes.Add("nd_id", Request.QueryString.Get("nd_id"));
                SectionServices sectionService = new SectionServices();
                UserManager um = new UserManager();
                if (pcs[0].Equals("0"))
                {
                    List<Section> listSection = sectionService.findAllSection();
                    foreach (Section section in listSection)
                    {
                        foreach (User user in um.findBySectionId(section.SectionId))
                        {
                            this.willSelectOneLbx.Items.Add(new ListItem(user.UserName, user.UserId.ToString()));
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < pcs.Length; i++)
                    {
                        if (pcs[i] != null && !pcs[i].Equals(""))
                        {
                            int sid = int.Parse(pcs[i]);
                            List<User> list = um.findBySectionId(sid);
                            foreach (User user in list)
                            {
                                this.willSelectOneLbx.Items.Add(new ListItem(user.UserName, user.UserId.ToString()));
                            }
                        }
                    }
                }
                if (opUser != null && opUser.Length > 0)
                {
                    for(int j = 0; j < this.willSelectOneLbx.Items.Count; j++)
                    {
                        ListItem item = willSelectOneLbx.Items[j];
                        for (int i = 0; i < opUser.Length; i++)
                        {
                            if (item.Value.Equals(opUser[i]))
                            {
                                this.selectedLbx.Items.Add(item);
                                this.willSelectOneLbx.Items.Remove(item);
                                j--;
                            }
                        }
                    }
                }
            }
        }
    }
    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        if (Request.Form.GetValues(this.selectedLbx.ClientID) != null && Request.Form.GetValues(this.selectedLbx.ClientID).Length > 0)
        {
            int ndid = int.Parse(((Button)sender).Attributes["nd_id"]);
            string[] uids = Request.Form.GetValues(this.selectedLbx.ClientID);
            string opUsers = "";
            for (int i = 0; i < uids.Length; i++)
            {
                if (i == uids.Length - 1)
                {
                    opUsers += uids[i];
                }
                else
                {
                    opUsers += (uids[i] + ",");
                }
            }
            NetDisktbServices ndService = new NetDisktbServices();
            if (ndService.AddCatalogOpUsers(opUsers, ndid) == 1)
            {
                Response.Redirect("NetDiskIndex.aspx");
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('添加人员失败，请重试！');", true);
            }
        }
    }
}
